<?php
namespace Common\TagLib;
/**
 * Class TagLibXcallinput
 * @since 1.0 2014-10-29 by sutroon
 */
class Utilhtml extends TagBase {

    protected $tags = array(
        'siteid' => array('attr' => 'attrs', 'close' => 0),
    );

    /**
     * 获取租户编号字段
     * @param $attr
     * @param $content
     * @return string
     * @since 1.0 2014-10-30 by sutroon
     * @example
     *  1.config.php: 'TAGLIB_BUILD_IN' => 'cx,xcallinput'
     *  2.<utilhtml:siteid value="{$loginData['siteID']}" />
     */
    public function _siteid($attr, $content) {
        $attr = $this->parseXmlAttr($attr, 'siteid');
        $isSuperAdmin = $this->tpl->get('isSuperAdmin');
        $val = $this->base_get_tpl_data($attr['value']);
        $table = isset($attr['table']) ? $attr['table'] : 'tuser';
        if ($isSuperAdmin) {
            $list = S('admin_siteid_names_' . $table);
            if (!$list) {
                $list = M($table)->where("userType='ADMIN'")->getFields(array('siteID', "concat(userName,'[',siteID,']') as userName"));
                S('admin_id_names_' . $table, $list, array('expire' => 15));
            }
            if ($list) {
                $list = array('' => '选择租户') + $list;
            }
            $str = '<select name="siteID" title="请选择租户" required>';
            foreach ($list as $k => $v) {
                $str .= '<option value="' . $k . '"' . ($val && $k == $val ? ' selected="selected"' : '') . '>' . $v . '</option>';
            }
            return $str . '</select>';
        } else {
            return '<input type="hidden" name="siteID" value="' . $val . '" />';
        }
    }

}
